package com.kesh.ltank.elements;

import android.graphics.Point;

import com.kesh.ltank.game.Utils;
import com.kesh.ltank.game.GlobalVars.AntiTankState;
import com.kesh.ltank.game.GlobalVars.BulletState;
import com.kesh.ltank.game.GlobalVars.Direction;
import com.kesh.ltank.game.GlobalVars.PlayObjects;

public class ATank {

	private Bullet tankBullet;
	private Point tankPos;
	private Direction tankDirection;
	private AntiTankState tankState;
	private int tankStateTime;

	public ATank() {
		tankBullet = new Bullet();
		tankPos = new Point(0,0);
		tankDirection=Direction.Up;
		tankState=AntiTankState.Exploded;
		tankStateTime=0;
	}

	public ATank clone(){
		ATank newATank= new ATank();
		newATank.tankBullet = tankBullet;
		newATank.tankPos = tankPos;
		newATank.tankDirection = tankDirection;
		newATank.tankState = tankState;
		newATank.tankStateTime=tankStateTime;
		return newATank;
	}
	
	public void fireTank(LTank lTank){
		if(tankStateTime>0){
			tankStateTime--;
		} else {
			PlayObjects lvlPlayField[][]=lTank.getLvlPlayField();
			tankStateTime=0;
			switch (tankBullet.getCurBulletState()) {
				case Exploded:
					tankBullet.resetBullet();
					tankState=AntiTankState.Exploded;
					break;
				case HitTank:
					tankBullet.resetBullet();
					tankState=AntiTankState.HitTank;
					break;
				case Fired:case InMotion:
					if(tankBullet.getCurBulletState()==BulletState.Fired){
						tankState=AntiTankState.Firing;
						tankBullet.createBullet(tankPos,tankDirection);
					} else {
						tankBullet.createBullet();
					}
					
					Point curBltPos = tankBullet.getCurBulletPos();
					Point nxtBltPos = Utils.getNextPosition(curBltPos,tankBullet.getCurBulletDirection());
					PlayObjects nxtPosObj= PlayObjects.OutOfBounds;
					if(Utils.positionWithinBounds(nxtBltPos)) nxtPosObj=lvlPlayField[nxtBltPos.x][nxtBltPos.y];
					switch (nxtPosObj) {
						case LTank_D:case LTank_L:case LTank_U:case LTank_R:
							tankBullet.setCurBulletState(BulletState.HitTank);
							break;
						case Brick:case Crystal:case Flag:case MBlock:case MMirror_D:case MMirror_L:case MMirror_R:case MMirror_U:case RMirror_D:case RMirror_L:case RMirror_R:case RMirror_U:case Steel:case ATank_D:case ATank_L:case ATank_R:case ATank_U:case OutOfBounds:
							tankBullet.setCurBulletState(BulletState.Exploded);
							break;
						default:
							tankBullet.setCurBulletState(BulletState.InMotion);
							break;
					}
					tankBullet.setCurBulletPos(nxtBltPos);
			}
		}
	}

	public Bullet getTankBullet() {
		return tankBullet;
	}

	public void setTankBullet(Bullet tankBullet) {
		this.tankBullet = tankBullet;
	}

	public Point getTankPos() {
		return tankPos;
	}

	public void setTankPos(Point tankPos) {
		this.tankPos = tankPos;
	}

	public Direction getTankDirection() {
		return tankDirection;
	}

	public void setTankDirection(Direction tankDirection) {
		this.tankDirection = tankDirection;
	}

	public AntiTankState getTankState() {
		return tankState;
	}

	public void setTankState(AntiTankState tankState) {
		this.tankState = tankState;
	}

	public int getTankStateTime() {
		return tankStateTime;
	}

	public void setTankStateTime(int tankStateTime) {
		this.tankStateTime = tankStateTime;
	}
	

}
